package com.heima.dome1;

import com.heima.domain.Student;

import java.util.ArrayList;
import java.util.Iterator;

public class lianxi5 {
    public static void main(String[] args) {

//        (1)创建5个学生对象存入ArrayList集合中
//        (2)打印最高分的学员姓名、年龄、成绩  [要求封装1个方法  参数是集合对象   返回值类型为Student]
//        (3)打印5个学生的总成绩和平均分		 [要求封装一个方法完成]
//        (4)打印不及格的学员信息及数量		 [要求封装一个方法完成]

        ArrayList<Student> list=new ArrayList<>();

        Student s1=new Student("小张",13,82);
        Student s2=new Student("小黄",13,77);
        Student s3=new Student("小李",13,55);
        Student s4=new Student("小红",13,40);
        Student s5=new Student("小杨",13,100);

        list.add(s1);
        list.add(s2);
        list.add(s3);
        list.add(s4);
        list.add(s5);

//        methon1(list);

        methon2(list);

//        methon3(list);




    }

    private static void methon3(ArrayList<Student> list) {
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            Student student = it.next();
            if (student.getScore()<60){
                System.out.println(student.toString());
            }
        }
    }

    private static void methon2(ArrayList<Student> list) {
        /*
        for循环遍历
         */
//        int fenshu=0;
//        for (int i = 0; i < list.size(); i++) {
//            fenshu=fenshu+list.get(i).getScore();
//        }
//        System.out.println("总分为："+fenshu);
//        System.out.println("平均分为："+fenshu/list.size());

        /*
        迭代器遍历
         */

        int scores=0;
        Iterator<Student> it = list.iterator();
        while (it.hasNext()){
            Student student = it.next();
            scores=scores+student.getScore();
        }
        System.out.println("总分为："+scores);
        System.out.println("平均分为："+scores/list.size());

    }

    private static void methon1(ArrayList<Student> list) {
        int maxscore=0;
        for (int i = 0; i < list.size()-1; i++) {
            if (list.get(i).getScore()<list.get(i+1).getScore()){
                maxscore=i+1;
            }else {
                maxscore=i;
            }
        }
        System.out.println(list.get(maxscore).toString());



    }
}
